﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Threading;

namespace Helper.Model
{
    public class TaskModel: ViewModelBase
    {
        public int id { get; set; }
        /** 名称 */
        public string name { get; set; }
        /** 类型 */
        public string type { get; set; }
        /** 描述 */
        public string describe{ get; set; }
        /** 奖励分数 */
        public int rewardPoints{ get; set; }
        /** 奖励内容 */
        public string rewardContent{ get; set; }
        /** 开始日期 */
        public DateTime startDateTime{ get; set; }
        /** 计划完成时间 */
        public double planTime{ get; set; }
        /** 进行时间 */
        public double runTime{ get; set; }
        /** 结束日期 */
        public DateTime endDateTime{ get; set; }
        /** 是否完成 */
        public bool isComplete{ get; set; }
        /**进度值*/
        public TaskModel(int id)
        {
            this.id = id;
            name = "Name";
            type = "Type";
            describe = "测试描述";
            rewardPoints = 10;
            rewardContent = "测试奖励内容";
            startDateTime = DateTime.Now;
            planTime = 0.5;
            runTime = 0;
            isComplete = false;
        }
        private bool m_IsChecked;
        public bool IsChecked
        {
            get
            {
                return m_IsChecked;
            }
            set
            {
                m_IsChecked = value;
                RaisePropertyChanged("IsChecked");
            }
        }
        /// <summary>
        /// 计时器 开启命令 
        /// </summary>
        DispatcherTimer timer;
        public ICommand OpenClick => new RelayCommand(() =>
        {
            if(startDateTime==null) startDateTime = DateTime.Now;
            RecordRunTime();
        });
        /// <summary>
        /// 开启计时器 记录 进行时间
        /// </summary>
        private void RecordRunTime()
        {
            Debug.WriteLine("IsChecked:" + IsChecked);
            if (timer == null)
            {
                timer = new DispatcherTimer();
                timer.Tick += delegate
                {
                    if (!IsChecked) timer.Stop();
                    if (runTime >= planTime*60*60*60) timer.Stop();
                    runTime+=10000;
                    Debug.WriteLine("runTime:" + runTime);
                };
                timer.Interval = new TimeSpan(0,0,0,1,0);
                timer.Start();
            }
            if (IsChecked) timer.Start();
            else timer.Stop();
        }
        public double ProgressValue
        {
            get
            {
                return GetValue();
            }
            set
            {
                ProgressValue = value;
                RaisePropertyChanged("ProgressValue");
            }
        }
     
        private double GetValue()
        {
            Debug.WriteLine("runTime:" + runTime +" :planTime "+ planTime * 60 * 60 * 60);

            double value = runTime / (planTime * 60 * 60 * 60);
            value = Math.Round(value, 3)*100;
            Debug.WriteLine("value:"+value);

            return value>=100?100:value;
        }
    }
}
